#version 400

struct Light{
    vec3 position;
};
struct Material{
    vec3 diffuse;
	vec3 specular;
	float shininess;
};

uniform Light light0;
uniform Material material;

varying vec4 pPosition;
varying vec3 pNormal;

layout (location = 0) out vec4 FragColor;

vec3 ambient=material.diffuse*0.5;
vec3 diffuse=material.diffuse*0.5;
vec3 specular=material.specular;
vec3 N=normalize(pNormal);
vec3 E=normalize(-vec3(pPosition));

void applyAmbient(){
	float NdotL=max(0.0,dot(N,E));
	ambient*=NdotL;
}

void applyLight(){
    vec3 L=normalize(light0.position-vec3(pPosition));
    vec3 H=normalize(L+E);
	float NdotL=max(0.0,dot(N,L));
	diffuse*=NdotL;
	float NdotH=max(0.0,dot(N,H));
	float Ispec=pow(NdotH,material.shininess+1.0);
	specular*=Ispec;
}

void main(){
    applyAmbient();
	applyLight();
	FragColor=vec4(ambient+diffuse+specular,1.0);
}
